<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceGuestList extends core {

    public function process() {
        $limit = $this->limit;
        $page = intval($_POST['page']);
        $user_id = intval($_POST['user_id']);
        $event_id = intval($_POST['event_id']);
        $attend = intval($_POST['attend']);
        $event = $this->db->get_row("SELECT * FROM events WHERE event_id=$event_id AND `user_id`='$user_id'");
        if ($event) {
            if ($page < 1) {
                $page = 1;
            }
            $from = ($page - 1) * $limit + 2;
            $total = 0;

            $data = $this->db->get_results("SELECT g.firstname, g.lastname, g.photo FROM guests  g, guest_event  ge WHERE g.guest_id=ge.guest_id AND ge.event_id={$event_id} AND ge.attend = {$attend} ORDER BY g.firstname asc LIMIT $from,$limit;");
            $total = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event_id AND `attend`={$attend}");
            $total->total = $total->total - 2;
            if (ceil($total->total / $limit) <= $page) {
                $end_paging = 1;
            } else {
                $end_paging = 0;
            }

            $html = '';
            foreach ($data as $guest) {
                if($guest->photo !=""){
                        $photo_src = '<img src="'.$guest->photo.'" class="wd-grp-member-event-ava"/>';
                    }else{
                        $photo_src = '<img src="img/no-ava.png" class="wd-grp-member-event-ava"/>';                        
                    }
                    $html.='
                            <div class="wd-grp-member-event">
                                '.$photo_src.'
                                <div class="wd-grp-member-event-name">'.$guest->firstname.' '.$guest->lastname.'</div>
                            </div>
                            
                            ';
            }

            $this->_JSONResponse(array('error' => 0, 'html' => $html, 'page' => $page + 1, 'end_paging' => $end_paging));
        } else {
            $this->_JSONResponse(array('error' => 1, 'msg' => "Event does not exist!"));
        }
    }

}

$processor = new webServiceGuestList();
$processor->process();
?>